2D Animated Orbits

Learn how to plot 2D animated orbits.

Now, let’s animate the orbit. Note that you have reset the argument of perigee to be 0 degrees.

Matplotlib’s FuncAnimation() takes a function and computes the next value given a range, plots the new value, waits a specified period of time, and then plots the next value. In your case, your animation is really just displaying the pre-computed X and Y coordinates of the orbit. You need to do some prep work to plot the orbit, plot the starting point of a red dot that will go around the orbit, and internally define an animate() function that accepts the incremental value i and returns (pos[i][1], pos[i][0]), so that the FuncAnimation() function can plot the new coordinates. Since pos[i] will give you the entire list of X or Y coordinates, you use the second [1] to get the X coordinates or [0] to get the Y coordinates of the nested list.

Code for animating a chart

The comma is important after red_dot; the comma means that red_dot is treated as a tuple and not just as a list of lists. FuncAnimation takes a lot of arguments:

  • Fig is the plot so the animation knows where to display the animation

  • Animate is the function that provides the animation

  • Frames is the source of data (the incremental i mentioned earlier) that provides the animation. In this case, since you are iterating through an array, you want a range, as large as the original time array, that provides ints so that the animation can iterate through each element of the array

  • Interval is the time delay between each iteration of the animation

  • Blitting is a 2D graphics technique that separates the background of the plot from the moving red dot. If this argument is True, the background is frozen in place, and only the red dot is re-animated each time. It should make the animation load and run faster because it is only re-computing the red dot’s coordinates, not re-displaying the entire screen

  • Repeat asks if the animation should play once and freeze, or continue indefinitely. There is also an optional repeat_delay parameter that can delay the repeat by the specified number of seconds There are a few other optional arguments that you did not use [2]. Finally, you will finish up with the plotting cleanup and running the code.

plt.plot(0, 0, 'bo', markersize=9, label="Earth")
plt.tick_params(
    axis='both',  # changes apply to both axes
    which='both',  # both major and minor ticks are affected
    bottom=False,  # ticks along the bottom edge are off
    top=False,  # ticks along the top edge are off
    labelbottom=False,  # labels along the bottom edge are off
    left=False,
    labelleft=False)
 
plt.legend(loc="upper right")
plt.title('Orbital Simulation')
plt.show()

Let’s see what you have created. This will be a static screenshot of the animated orbit.

Static picture of a 2D animated orbit

Here is the actual GIF; you will get to saving GIFs later!

2D animated orbit GIF
2D animated orbit GIF

Congratulations on your first animated plot! If you wanted to be really funny, you could use Matplotlib’s dark_background style and re-color the dot so that the background is black, the dot is white, and it almost looks like space. Using the dark background brings out the previously hidden white gridlines, so you will need to turn those off, too!

Cleaning up the space GIF

Let’s see some space!

2D, animated space

Since you have not gotten to saving GIFs yet, here is what the animated orbit would look like:

widget

2D Static Orbits

3D Static and Animated Orbits